package com.sskj.lib.base;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.support.multidex.MultiDexApplication;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.launcher.ARouter;
import com.hjq.toast.ToastUtils;
import com.hjq.toast.style.ToastQQStyle;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.shizhefei.view.coolrefreshview.IPullHeaderFactory;
import com.shizhefei.view.coolrefreshview.PullHeader;
import com.sskj.common.BuildConfig;
import com.sskj.common.base.App;
import com.sskj.common.router.service.CommonService;
import com.sskj.common.util.SPUtil;
import com.sskj.lib.BaseHttpConfig;
import com.sskj.lib.SPConfig;
import com.sskj.lib.box.lifecycle.FingerActivityLifecycleCallbacks;
import com.sskj.lib.router.service.LibService;
import com.sskj.lib.widget.MyClasscsFooter;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class MyApplication extends MultiDexApplication {
    @Autowired
    CommonService commonService;
    @Autowired
    LibService libService;

    public static IWXAPI iwxapi;

    @Override
    public void onCreate() {
        super.onCreate();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
            closeAndroidPDialog();
        }
        ARouter.init(this);
        ARouter.openDebug();
//        Recovery.getInstance()
//                .debug(true)
//                .recoverInBackground(false)
//                .recoverStack(true)
//                .mainPage((Class<? extends Activity>) ARouter.getInstance().build(RConfig.APP_MAIN).navigation())
//                .recoverEnabled(true)
//                .callback(null)
//                .silent(false, Recovery.SilentMode.RECOVER_ACTIVITY_STACK)
//                .init(this);
        if (BuildConfig.DEBUG) {
        } else {
            Thread.setDefaultUncaughtExceptionHandler((t, e) -> System.out.println(e));
        }
        registerToWX();

        ARouter.getInstance().inject(this);
        commonService = new CommonService();
        commonService.initOkGoHttp(this, BuildConfig.DEBUG);
        ToastUtils.init(this);
        ToastUtils.initStyle(new ToastQQStyle());
        SmartRefreshLayout.setDefaultRefreshHeaderCreator((context, layout) -> new MaterialHeader(context));
        SmartRefreshLayout.setDefaultRefreshFooterCreator((context, layout) -> new MyClasscsFooter(context));
        CoolRefreshView.setPullHeaderFactory(new IPullHeaderFactory() {
            @Override
            public PullHeader made(Context context) {
                com.shizhefei.view.coolrefreshview.header.MaterialHeader commonHeader = new com.shizhefei.view.coolrefreshview.header.MaterialHeader(context);
                return commonHeader;

            }

            @Override
            public boolean isPinContent() {
                return true;
            }
        });

//        LanguageUtil.changeAppLanguage(this, Locale.CHINA, false);


        App.INSTANCE.registerActivityLifecycleCallbacks(new FingerActivityLifecycleCallbacks());
    }

    private void registerToWX() {
        iwxapi = WXAPIFactory.createWXAPI(this, BaseHttpConfig.APP_ID, true);
        registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                iwxapi.registerApp(BaseHttpConfig.APP_ID);
            }
        }, new IntentFilter(ConstantsAPI.ACTION_REFRESH_WXAPP));
    }

    private void closeAndroidPDialog() {
        try {
            Class aClass = Class.forName("android.content.pm.PackageParser$Package");
            Constructor declaredConstructor = aClass.getDeclaredConstructor(String.class);
            declaredConstructor.setAccessible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            Class cls = Class.forName("android.app.ActivityThread");
            Method declaredMethod = cls.getDeclaredMethod("currentActivityThread");
            declaredMethod.setAccessible(true);
            Object activityThread = declaredMethod.invoke(null);
            Field mHiddenApiWarningShown = cls.getDeclaredField("mHiddenApiWarningShown");
            mHiddenApiWarningShown.setAccessible(true);
            mHiddenApiWarningShown.setBoolean(activityThread, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String getToken() {
        return SPUtil.get(SPConfig.TOKEN, "");
    }

    public String getAccount() {
        return SPUtil.get(SPConfig.ACCOUNT, "");
    }

}
